package com.lx.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lx.entity.LxEnv;
import com.lx.entity.LxUser;
import com.lx.service.LxEnvService;
import com.lx.service.impl.LxEnvServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 赵敬轩
 * @since 2021-09-12
 */
@RestController
@RequestMapping("/lx-env")
public class LxEnvController {
    @Autowired
    private LxEnvServiceImpl lxEnvService;

    @CrossOrigin
    @GetMapping("/getAll")
    public String getAllEnv(Integer page,Integer limit){
        IPage<LxEnv> lxEnvIPage = lxEnvService.selectAllEnv(page,limit);
        List<LxEnv> list = lxEnvService.list();
        System.out.println(list);
        Map<String,Object> resultData = new HashMap<>();
        resultData.put("code",0);
        resultData.put("msg",null);
        resultData.put("data",lxEnvIPage.getRecords());
        resultData.put("count",list.size());
        return JSON.toJSONString(resultData);
    }

    @CrossOrigin
    @GetMapping("/delete")
    public String deleteEnv(String id){
        HashMap<String,Object> resultData = new HashMap<>();
        System.out.println(id);
        boolean deleteEnvId = lxEnvService.removeById(id);
        if (deleteEnvId){
            resultData.put("msg","删除成功");
        }else{
            resultData.put("msg","删除失败");
        }
        return JSON.toJSONString(resultData);
    }

    @CrossOrigin
    @PostMapping("/save")
    public String saveEnv(@RequestBody LxEnv lxEnv){
        System.out.println("lxEnv"+lxEnv);
        HashMap<String,Object> resultData = new HashMap<>();
        System.out.println(lxEnv);
        boolean save = lxEnvService.saveOrUpdate(lxEnv);
        if (save)
            resultData.put("msg","信息保存成功");
        else
            resultData.put("msg","信息保存失败");
        return JSON.toJSONString(resultData);
    }



}
